/**
 * @class   wosh::BundleLoader
 * @brief   The class manage loading of Bundles
 *          
 *          
 *          
 ****************************************************************************
 * @version $Id: BundleLoader.h 2488 2010-05-21 01:48:27Z alex $
 * @author  Alessandro Polo
 ****************************************************************************/
/* Copyright (c) 2007-2010, WOSH - Wide Open Smart Home 
 * by Alessandro Polo - OpenSmartHome.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the OpenSmartHome.com WOSH nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Alessandro Polo ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Alessandro Polo BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ****************************************************************************/

#ifndef __WOSH_Core_BundleLoader_H__
 #define __WOSH_Core_BundleLoader_H__

 #include <woshDefs.h>
 #include <core/Object.h>
 #include <core/Bundle.h>
 #include <core/Containers.h>
 #include <core/ObjectAllocator.h>

 
namespace wosh {


class BundleAction
 {
	public:
		enum ACTION_TYPE {
			ACTION_UNKNOWN	= 0x00,
			ACTION_LOAD		= 0x01,
			ACTION_START	= 0x02,
			ACTION_STOP		= 0x04,
			ACTION_UNLOAD	= 0x08
		 };
		enum ACTION_RESULT {
			RESULT_UNKNOWN	= 0x00,
			RESULT_SUCCEDED	= 0x01,
			RESULT_FAILED	= 0x02,
			RESULT_DELAYED	= 0x04
		 };

	public:
		BundleAction( LogContext& log ) : Log(log), executedTs(0), result(RESULT_UNKNOWN), result_ret(WRET_ERR_UNKNOWN) { }
		virtual ~BundleAction() { }

	public:
		virtual ACTION_TYPE getType() const = 0;
		virtual void execute() = 0;
		WRESULT executeAsynch( unsigned long timeout = LIMIT_ULONG_MAX );

		static WRESULT applySettingTo( Bundle* bundle );
		static BundleAction* fromString( LogContext& log, const std::string& dataline );
		static BundleAction* fromBundleState( LogContext& log, Bundle::BUNDLE_STATE state );

	protected:
		void succeded();
		void failed( WRESULT ret = WRET_ERR_INTERNAL, ACTION_RESULT result = RESULT_FAILED );

		ListT<Bundle*>& getBundles();
		BundleManager* getBundleManager();
		Bundle* getBundleByName_();
		Mutex& getStateTransitionMux();
		WRESULT validateTransitionRequirements( Bundle* bundle, Bundle::BUNDLE_STATE state_scope );
		WRESULT validateTransitionState( Bundle* bundle, Bundle::BUNDLE_STATE new_state, int allowed_states_mask );
		WRESULT validateConflictsRequirements( Bundle* bundle, Bundle::BUNDLE_STATE state_scope );


	public:
		LogContext& Log;
		std::string bundle_name;

		long executedTs;
		ACTION_RESULT result;
		WRESULT result_ret;
};

class BundleActionLoad : public BundleAction
 {
	public:
		BundleActionLoad( LogContext& log ) : BundleAction(log), apply_settings(true) { }
		virtual ~BundleActionLoad() { }

	public:
		virtual void execute();
		ACTION_TYPE getType() const { return ACTION_LOAD; }

	public:
		ObjectTypeInfo bundle_type;
		bool apply_settings;
};

class BundleActionStart : public BundleAction
 {
	public:
		BundleActionStart( LogContext& log ) : BundleAction(log) { }
		virtual ~BundleActionStart() { }

	public:
		virtual void execute();
		ACTION_TYPE getType() const { return ACTION_START; }

};

class BundleActionStop : public BundleAction
 {
	public:
		BundleActionStop( LogContext& log ) : BundleAction(log) { }
		virtual ~BundleActionStop() { }

	public:
		virtual void execute();
		ACTION_TYPE getType() const { return ACTION_STOP; }

};

class BundleActionUnLoad : public BundleAction
 {
	public:
		BundleActionUnLoad( LogContext& log ) : BundleAction(log) { }
		virtual ~BundleActionUnLoad() { }

	public:
		virtual void execute();
		ACTION_TYPE getType() const { return ACTION_UNLOAD; }

};


typedef ListT<BundleAction*> tBundleActionList;


class BundleLoader : public Object
 {
	WOSH_CLASSNAME(wosh::BundleLoader)
	WOSH_CLASS_ABSTRACT(false)

	public:
		BundleLoader();

		virtual ~BundleLoader();


	public:
		WRESULT loadFromArgs( int argc, char **argv );

		// reentrant
		WRESULT loadActionsFile( const std::string& filename );

		// reentrant
		WRESULT loadActionsFolder( const std::string& folder, const std::string& pattern = ".load" );

	public:
		// reentrant
		WRESULT executeActions( BundleAction::ACTION_TYPE type ) {
			return executeActions(bundleActions, type);
		 }

		WRESULT createActions( tBundleActionList& actions, Bundle::BUNDLE_STATE state_transition );

		WRESULT executeActions( tBundleActionList& actions, BundleAction::ACTION_TYPE type = BundleAction::ACTION_UNKNOWN );

	public:
		const tBundleActionList& getActions() const	{ return this->bundleActions; }
		tBundleActionList& getActions()				{ return this->bundleActions; }

	protected:
		WRESULT parseLine_( const std::string& dataLine );

	protected:
		tBundleActionList bundleActions;

		mutable LogContext Log;


}; // class def

}; // namespace wosh

#endif //__WOSH_Core_BundleLoader_H__
